package com.example.demo.mapper;

import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    @Select("select * from userinfo")
    List<UserInfo> getUserInfo();

    @Select("select * from userinfo where id = #{id}")
    UserInfo getUserInfo1(Integer id);

    @Select("select * from userinfo where id = #{num}")
    UserInfo getUserInfo11(@Param("num") Integer id);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, password, age, gender) " +
            "values (#{username}, #{password}, #{age}, #{gender})")
    Integer insert(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, password, age, gender) " +
            "values (#{info.username}, #{info.password}, #{info.age}, #{info.gender})")
    Integer insert1(@Param("info") UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set password = #{password}, age = #{age}, gender = #{gender} where id = #{id}")
    Integer update(UserInfo userInfo);

    // 查询
    // 起别名
    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userinfo")
    List<UserInfo> getUserInfo2();

    // 结果映射
    @Results(id = "UserInfoResultMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> getUserInfo3();

    @Select("select id, username, password, delete_flag, create_time, update_time from userinfo")
    @ResultMap(value = "UserInfoResultMap")
    List<UserInfo> getUserInfo4();

    // 开启自动驼峰转换
    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from userinfo")
    List<UserInfo> getUserInfo5();

    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag, create_time, update_time from userinfo " +
            "where id = #{id}")
    UserInfo queryById1(Integer id);

    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag, create_time, update_time from userinfo " +
            "where id = ${id}")
    UserInfo queryById2(Integer id);

    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag, create_time, update_time from userinfo " +
            "where username = #{username}")
    UserInfo queryByUsername1(String username);

    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag, create_time, update_time from userinfo " +
            "where username = ${username}")
    UserInfo queryByUsername2(String username);
}
